import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../views/login/index.vue')
const Layout = () => import('../views/layout/index.vue')
const Article = () => import('../views/article/index.vue')
const EditArticle = () => import('../views/article/edit.vue')
const AddArticle = () => import('../views/article/add.vue')
const Comment = () => import('../views/comment/index.vue')
const Fans = () => import('../views/fans/index.vue')
const Settings = () => import('../views/settings/index.vue')
const Image = () => import('../views/image/index.vue')
const Page404 = () => import('../views/Page404/index.vue')

// // 导入组件
// const Login = () => import('@/views/login')
// const Layout = () => import('@/views/Layout')
// const Welcome = () => import('@/views/welcome')
// const NotFound = () => import('@/views/404')
// const Article = () => import('@/views/article')
// const Image = () => import('@/views/image')
// const Publish = () => import('@/views/publish')
// const Comment = () => import('@/views/comment')
// const Fans = () => import(/* webpackChunkName: "fans" */'@/views/fans')
// const Setting = () => import('@/views/setting')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    children: [
      {
        // 内容管理
        path: '/article',
        name: 'Article',
        component: Article
      },
      {
        // 素材管理
        path: '/image',
        name: 'Image',
        component: Image
      },
      {
        // 编辑文章
        // 动态路由  path: '/editArticle/:id',
        path: '/editArticle/:id',
        name: 'editArticle',
        component: EditArticle
      },
      {
        // 发布文章
        path: '/addArticle',
        name: 'AddArticle',
        component: AddArticle
      },
      {
        // 评论管理
        path: '/comment',
        name: 'Comment',
        component: Comment
      },
      {
        // 粉丝管理
        path: '/fans',
        name: 'Fans',
        component: Fans
      },
      {
        // 粉丝管理
        path: '/settings',
        name: 'Settings',
        component: Settings
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '*',
    name: 'Page404',
    component: Page404
  }
]
const router = new VueRouter({
  routes
})
// 路由导航守卫
//   所有的路由页面中跳转，都会先进入到这个函数中来。
// to: 要跳入的路由信息。  你要去哪里
// from: 要跳出的路由信息。你从哪里来
// next: 放行方法
//   - next() : 正确跳转。允许进入到 to 路由
//   - next(路由地址) : 不允许去to，而转去指定的路由地址
//  /image /editArticle/:id /addArticle /comment /fans /settings
router.beforeEach((to, from, next) => {
  console.log('路由导航守卫:', to, from, next)
  // 如果你没有登陆，则不让进入 主页
  if (to.path === '/' || to.path === '/articles' || to.path === '/image' || to.path === '/editArticle/:id' || to.path === '/addArticle' || to.path === '/comment' || to.path === '/fans' || to.path === '/settings') {
    // 检查是否有token
    const token = localStorage.getItem('tokenStr')
    if (token) {
      next()
    } else {
      // 回登陆页
      alert('没有token，不能访问主页')
      next('/login')
    }
  } else {
    next()
  }
})
export default router
